<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Libs\Filter\Filter;
use App\Models\Order;
use App\Services\IOrderService;
use App\Services\IRefundService;
use Illuminate\Http\Request;

class OrderController extends BaseController
{

    /**
     * 所有订单
     * @param Request $request
     */
    public function all(Request $request,IOrderService $orderService)
    {
        $filter = Filter::instance()
            ->HTML('订单编号：')
            ->input('order_number')
            ->HTML('下单用户：')
            ->input('me_username')
            ->HTML('订单地址：')
            ->input('customer_address')
            ->br()->br()
            ->HTML('服务类型：')
            ->select('service_type',array(''=>'请选择')+Params::$MERCHANT_SERVICE_TYPE,null,array('style'=>'width:150px'))
            ->HTML('订单状态：')
            ->select('merchant_status',array(''=>'请选择')+Params::$ORDER_MERCHANT_STATUS,null,array('style'=>'width:150px'))
            ->HTML('订单异常状态：')
            ->select('except_status',array(''=>'请选择')+Params::$ORDER_EXCEPT_STATUS,null,array('style'=>'width:150px'))
            ->br()->br()
            ->HTML('下单时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
        $list = $orderService->all($filter->get_where());
    }else{
        $list = $orderService->all();
    }
        return view('order/all',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 仲裁订单
     * @param Request $request
     */
    public function arbitrateList(Request $request,IOrderService $orderService)
    {
        $filter = Filter::instance()
            ->HTML('订单编号：')
            ->input('order_number')
            ->HTML('下单用户：')
            ->input('me_username')
            ->HTML('订单地址：')
            ->input('customer_address')
            ->br()->br()
            ->HTML('服务类型：')
            ->select('service_type',array(''=>'请选择')+Params::$MERCHANT_SERVICE_TYPE,null,array('style'=>'width:150px'))
            ->HTML('申请时间：')
            ->date('arbitrate_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $orderService->arbitrateList($filter->get_where());
        }else{
            $list = $orderService->arbitrateList();
        }
        return view('order/arbitrateList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 投诉订单
     * @param Request $request
     */
    public function complainList(Request $request,IOrderService $orderService)
    {
        $filter = Filter::instance()
            ->HTML('订单编号：')
            ->input('order_number')
            ->HTML('下单用户：')
            ->input('me_username')
            ->HTML('订单地址：')
            ->input('customer_address')
            ->br()->br()
            ->HTML('服务类型：')
            ->select('service_type',array(''=>'请选择')+Params::$MERCHANT_SERVICE_TYPE,null,array('style'=>'width:150px'))
            ->HTML('订单状态：')
            ->select('merchant_status',array(''=>'请选择')+Params::$ORDER_MERCHANT_STATUS,null,array('style'=>'width:150px'))
            ->HTML('订单异常状态：')
            ->select('except_status',array(''=>'请选择')+Params::$ORDER_EXCEPT_STATUS,null,array('style'=>'width:150px'))
            ->br()->br()
            ->HTML('投诉时间：')
            ->date('oc_add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $orderService->complainList($filter->get_where());
        }else{
            $list = $orderService->complainList();
        }
        return view('order/complainList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 查看订单详情
     */
    public function show(Order $order,Request $request)
    {
        $order->load(['offers','status','complain','detail','goods','master.stat']);
        return view('order/show',['order'=>$order]);
    }

    /**
     * 查看师傅报价
     */
    public function showOffer(Order $order,Request $request)
    {
        $order->load(['offers','status','complain','offers.master','offers.master.wallet']);
        return view('order/showOffer',['order'=>$order]);
    }

    /**
     * 查看服务节点
     */
    public function showStatus(Order $order,Request $request)
    {
        $order->load(['offers','status','complain']);
        return view('order/showStatus',['order'=>$order]);
    }

    /**
     * 查看申请退款
     */
    public function showRefund(Order $order,Request $request)
    {
        $order->load(['offers','status','complain','refunds']);
        return view('order/showRefund',['order'=>$order]);
    }

    /**
     * 查看介入仲裁
     */
    public function showArbitrate(Order $order,Request $request,IRefundService $refundService)
    {
        if ($request->isMethod('post')){
            $refundService->applyArbitrate($request->input('refund_id'),$request->input('arbitrate_result_type'),$request->input('arbitrate_result_reason'),$request->input('arbitrate_result_amount'));
        }

        $order->load(['offers','status','complain']);
        return view('order/showArbitrate',['order'=>$order,
            'result_agree_full'=>Params::ORDER_ARBITRATE_RESULT_AGREE_FULL,
            'result_agree_part'=>Params::ORDER_ARBITRATE_RESULT_AGREE_PART,
            'result_agree_disagree'=>Params::ORDER_ARBITRATE_RESULT_DISAGREE,
        ]);
    }

    /**
     * 查看投诉处理
     */
    public function showComplain(Order $order,Request $request,IRefundService $refundService)
    {
        if ($request->isMethod('post')){
            $refundService->applyComplain($request->input('complain_id'),$request->input('oc_handle_result'),$request->input('oc_handle_explain'),$request->input('oc_punish_amount'));
        }

        $order->load(['offers','status','complain','master']);
        return view('order/showComplain',['order'=>$order,
            'result_agree'=>Params::COMPLAIN_RESULT_AGREE,
            'result_disagree'=>Params::COMPLAIN_RESULT_DISAGREE,
        ]);
    }
}
